package com.doctor.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.doctor.entity.Doctor;
import com.doctor.exception.ServiceException;
import com.doctor.repository.DoctorRepository;
import com.doctor.service.DoctorService;

@Service
@Transactional
public class DoctorServiceImpl implements DoctorService {

    @Resource
    private DoctorRepository doctorRepository;
    
    @Override
    public Page<Doctor> findAll(PageRequest pageRequest) {
        return doctorRepository.findAll(pageRequest);
    }

    @Override
    public Doctor findById(Long id) {
        return doctorRepository.findOne(id);
    }

    @Override
    public void saveOrUpdate(Doctor doctor) {
        if(doctor == null){
            throw new ServiceException("医师不能为空");
        }
        doctorRepository.saveAndFlush(doctor);
    }

    @Override
    public void delete(Long id) {
        doctorRepository.delete(id);
    }

    @Override
    public List<Doctor> findAllMan() {
        return doctorRepository.findAll();
    }

	@Override
//	@Cacheable(value="doctor", key="#deparmentId")
	public Page<Doctor> findByDepartmentId(long deparmentId, Pageable pageable) {
		return doctorRepository.findByDepartmentId(deparmentId,pageable);
	}

}
